function scen_list = build_random_scenarios(load_buses, pv_buses, N, varargin)
% 随机生成 N 个场景：每个场景给出每个负荷母线、每个光伏母线的缩放因子
% 调用方式 1（4个位置参数，opts为结构体）：
%   opts = struct('seed',2025,'load_minmax',[0.2 1.0],'pv_minmax',[0.1 1.0],'name_prefix','rand');
%   scen_list = build_random_scenarios([2 3 4],[5 6 7],30, opts);
%
% 调用方式 2（Name-Value）：
%   scen_list = build_random_scenarios([2 3 4],[5 6 7],30, ...
%       'seed',2025,'load_minmax',[0.2 1.0],'pv_minmax',[0.1 1.0],'name_prefix','rand');

    %——基础形参校验——
    validateattributes(load_buses, {'double'},{'row','integer','positive'});
    validateattributes(pv_buses,   {'double'},{'row','integer','positive'});
    validateattributes(N,          {'double'},{'scalar','integer','positive'});

    %——默认值——
    seed        = 2025;
    load_minmax = [0.2 1.0];
    pv_minmax   = [0.1 1.0];
    name_prefix = 'rand';

    %——解析第4个参数（结构体）或 Name-Value 对——
    if ~isempty(varargin)
        if isstruct(varargin{1}) && numel(varargin)==1
            o = varargin{1};
            if isfield(o,'seed'),        seed        = o.seed;        end
            if isfield(o,'load_minmax'), load_minmax = o.load_minmax; end
            if isfield(o,'pv_minmax'),   pv_minmax   = o.pv_minmax;   end
            if isfield(o,'name_prefix'), name_prefix = o.name_prefix; end
        else
            p = inputParser;
            addParameter(p,'seed',        seed);
            addParameter(p,'load_minmax', load_minmax);
            addParameter(p,'pv_minmax',   pv_minmax);
            addParameter(p,'name_prefix', name_prefix);
            parse(p, varargin{:});
            seed        = p.Results.seed;
            load_minmax = p.Results.load_minmax;
            pv_minmax   = p.Results.pv_minmax;
            name_prefix = p.Results.name_prefix;
        end
    end

    %——随机数种子——
    rng(seed,'twister');
    Llo = load_minmax(1); Lhi = load_minmax(2);
    Plo = pv_minmax(1);   Phi = pv_minmax(2);

    %——生成场景——
    scen_list = cell(1, N);
    nL = numel(load_buses);
    nP = numel(pv_buses);
    for k = 1:N
        ls = [load_buses(:), Llo + (Lhi-Llo) * rand(nL, 1)];  % 各负荷母线缩放
        ps = [pv_buses(:),   Plo + (Phi-Plo) * rand(nP, 1)];  % 各光伏母线缩放
        scen_list{k} = struct( ...
            'load_scale', ls, ...
            'pv_scale',   ps, ...
            'name',       sprintf('%s_%03d', name_prefix, k) ...
        );
    end
end
